package com.tanhua.service;

import com.baidu.face.BaiduFaceTemplate;
import com.oss.OssTemplate;
import com.tanhua.domain.UserInfo;
import com.tanhua.dubbo.UserInfoAPI;
import com.tanhua.exception.MyException;
import com.tanhua.vo.ErrorResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class UserInfoService {

    @DubboReference
    private UserInfoAPI userInfoAPI;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private BaiduFaceTemplate baiduFaceTemplate;

    public  void save(UserInfo userInfo){
        userInfoAPI.save(userInfo);
    }



    public void uploadHead(MultipartFile headPhoto, Long userId) throws IOException {
        //1 上传头像到aliyun
        String imageURL = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        //2 百度人脸检测
        boolean detect = baiduFaceTemplate.detect(imageURL);
        if (!detect){
//            throw new RuntimeException("人脸检测失败");
            throw new MyException(ErrorResult.faceError());
        }
        //3 远程调用更新数据库
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setAvatar("https://21java.oss-cn-beijing.aliyuncs.com/2024/03/22/11221f5b-8c4c-40af-ae11-b7ff2586a2a3.jpg");
        userInfoAPI.update(userInfo);
    }

    public UserInfo findById(Long userId) {
        return userInfoAPI.findById(userId);

    }

    public void update(UserInfo userInfo) {
        userInfoAPI.update(userInfo);
    }
}
